var c = require('../lib/common.js');
var cf = require('../lib/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    status: 2,
    beizhu: '暂无',
    action: 2,
    urlo: '/images/upimg.png',
    urlt: '/images/upimg.png',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    c.verlogin(this);
    var action = this.data.action;
    var action = typeof(options) == 'object' ? 2 : options;
    wx.showLoading({
      title: '加载中...',
    })
    // var rid = wx.getStorageSync('userinfo').id;
    var rid = 9;
    var page = this.data.page;
    this.setData({
      page: page + 1
    });

    c.ajax(
      cf.hostapi + 'getriderOrderList', {
        rid: rid,
        page: page,
        status: action
      },
      res => {
        console.log(res);
        if (res.data.retcode == 200) {
          wx.hideLoading();
          var list = res.data.data;
        };



        var interval = setInterval(() => {
          for (var i = 0; i < list.length; i++) {
            var timestamp = Date.parse(new Date());
            if (list[i]['status'] == 3) {
              //还没有到过期时间
              if (list[i]['out_time'] >= parseInt(timestamp / 1000)) {
                // console.log('out_time----' + list[i]['out_time'] + '---------' + parseInt(timestamp / 1000));
                var xiangjian = list[i]['out_time'] - parseInt(timestamp / 1000);
                var miao = (xiangjian % 60);
                var fenz = parseInt(xiangjian / 60);
                list[i]['daojishi'] = '倒计时：' + fenz + '分:' + miao + '秒';
                list[i]['guoqi'] = 0;
                that.setData({
                  list: list
                })
                //此订单已经过期
              } else {
                var xiangjian = parseInt(timestamp / 1000) - list[i]['out_time'];
                // console.log(xiangjian);
                var hurs = parseInt(xiangjian / 3600);
                var hursm = (xiangjian % 3600);
                var fenz = parseInt(hursm / 60);
                var miam = (xiangjian % 60);
                list[i]['daojishi'] = '已过期:' + hurs + '时:' + fenz + '分:' + miam + '秒';
                list[i]['guoqi'] = 1;
                that.setData({
                  list: list
                })
              }
            }
          }
        }, 1000)
        this.setData({
          interval: interval
        })
        if (action != 3) {
          var end = interval;
          for (var i = 0; i <= end; i++) {
            clearInterval(i);
          }
        }
        this.setData({
          list: list,
          action: action
        })

      }
    );

  },

  setTimeout: function() {
    var interval = setTimeout(() => {
      for (var i = 0; i < list.length; i++) {
        var timestamp = Date.parse(new Date());
        if (list[i]['status'] == 3) {
          //还没有到过期时间
          if (list[i]['out_time'] >= parseInt(timestamp / 1000)) {
            // console.log('out_time----' + list[i]['out_time'] + '---------' + parseInt(timestamp / 1000));
            var xiangjian = list[i]['out_time'] - parseInt(timestamp / 1000);
            var miao = (xiangjian % 60);
            var fenz = parseInt(xiangjian / 60);
            list[i]['daojishi'] = '倒计时：' + fenz + '分:' + miao + '秒';
            list[i]['guoqi'] = 0;
            that.setData({
              list: list
            })
            //此订单已经过期
          } else {
            var xiangjian = parseInt(timestamp / 1000) - list[i]['out_time'];
            // console.log(xiangjian);
            var hurs = parseInt(xiangjian / 3600);
            var hursm = (xiangjian % 3600);
            var fenz = parseInt(hursm / 60);
            var miam = (xiangjian % 60);
            list[i]['daojishi'] = '已过期:' + hurs + '时:' + fenz + '分:' + miam + '秒';
            list[i]['guoqi'] = 1;
            that.setData({
              list: list
            })
          }
        }
      }
    }, 1000)
  },


  userinfo: function(e) {
    c.clicklogin(e, this);
  },
  orderclick: function(e) {
    var action = e.currentTarget.dataset.action;
    this.setData({
      page: 0,
      status: action
    })
    this.onLoad(action);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    var uid = wx.getStorageSync('userinfo').id;
    var action = that.data.action;
    var page = that.data.page + 1;
    that.setData({
      page: page + 1
    })
    c.ajax(
      cf.hostapi + 'getOrderList', {
        uid: uid,
        page: page,
        status: action
      },
      res => {
        if (res.data.retcode == 200) {
          wx.hideLoading();
          var list = that.data.list;
          var listn = list.concat(res.data.data);
          console.log(list);
          that.setData({
            list: listn,
            action: action
          })
        };
      }
    );
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  startOrder(e) {
    var that = this;
    wx.showLoading({
      title: '开始中...',
    })
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    c.ajax(
      cf.hostapi + 'startOrder', {
        order_id: id
      },
      res => {
        if (res.data.retcode == 200) {
          wx.hideLoading();
          wx.showToast({
            title: '开始成功',
            image: '/images/dui.png'
          })
          var list = that.data.list;
          list.splice(index, 1);
          console.log(list);
          that.setData({
            list: list
          })
        };
      }
    )
  },
  revokeOrder(e) {
    var that = this;
    wx.showLoading({
      title: '退单中...',
    })
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    c.ajax(
      cf.hostapi + 'redRevokeOrder', {
        order_id: id
      },
      res => {
        if (res.data.retcode == 200) {
          wx.hideLoading();
          wx.showToast({
            title: '退单成功',
            image: '/images/dui.png'
          })
          var list = that.data.list;
          list.splice(index, 1);
          console.log(list);
          that.setData({
            list: list
          })
        };
      }
    )
  },
  cuccess: function(e) {
    var order_id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var interval = this.data.interval;
    var that = this;
    console.log(order_id);
    c.ajax(
      cf.hostapi + 'OverOrder', {
        order_id: order_id
      },
      res => {
        if (res.data.retcode == 200) {
          wx.hideLoading();
          wx.showToast({
            title: '操作成功',
            image: '/images/dui.png'
          })
          var list = that.data.list;
          list.splice(index, 1);
          console.log(list);
          that.setData({
            list: list
          })
        };
      }
    )

    for (var i = 0; i <= interval; i++) {
      clearInterval(i);
    }
  },
  makecall: function(e) {
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  previewImage: function(e) {
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  give_pic: function(e) {
    var interval = this.data.interval;
    var list = this.data.list;
    var orderid = e.currentTarget.dataset.orderid;
    var index = e.currentTarget.dataset.index;
    for (var i = 0; i <= interval; i++) {
      clearInterval(i);
    }
    c.tuup(this, 'urlo', 'give_pic', orderid, list, index);
  },
  tack_pic: function(e) {
    var interval = this.data.interval;
    var list = this.data.list;
    var orderid = e.currentTarget.dataset.orderid;
    var index = e.currentTarget.dataset.index;
    for (var i = 0; i <= interval; i++) {
      clearInterval(i);
    }
    c.tuup(this, 'urlt', 'tack_pic', orderid, list, index);
  },
  previewImage: function(e) {
    var url = e.currentTarget.dataset.url;
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: [url] // 需要预览的图片http链接列表
    })
  },
  dingwei:function(e){
    var id=e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/addresstime/addresstime?addressid='+id,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  }
})